Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import matplotlib.pyplot as plt
import numpy as np

def centers(data,k):
    n = np.shape(data)[1]
    center = np.mat(np.zeros((k,n)))
    for i in range(n):
        mini = np.min(data[:,i])
        rangei = np.max(data[:,i]) - mini
        center[:,i] = np.mat(np.ones((k,1))) + np.random.rand(k,1)*rangei
    return center

def ab_distance(a,b):
    return np.sqrt(np.sum(np.square(a-b)))

def k_means(data,k,disfunc=ab_distance):
    m = np.shape(data)[0]
    adismatrix = np.mat(np.zeros((m,2)))
    center = centers(data,k)
    clusterchange = True
    while clusterchange:
        clusterchange = False
        for i in range(m):
            dismin = np.inf
            indexmin = -1
            for j in range(k):
                distanceij = disfunc(center[j,:],data[i,:])
                if distanceij <= dismin:
                    dismin = distanceij
                    indexmin = j
            if adismatrix[i,0] != indexmin:
                clusterchange = True
                adismatrix[i,:] = indexmin, dismin**2
        for cent in range(k):
            ptsincluster = data[np.nonzero(adismatrix[:,0].A==cent)[0]]
            center[cent,:] = np.mean(ptsincluster,axis=0)
    return center,adismatrix


def showc(data,k,center,adismatrix):
    m,n = data.shape
    mark = ['om', 'og', 'or', 'oy', 'oc', 'ob']
    for i in range(m):
        markindex = int(adismatrix[i, 0])
        plt.plot(data[i, 0], data[i, 1], mark[markindex])
    for i in range(k):
        plt.plot(center[i, 0], center[i, 1], '*',markeredgecolor='black' , markersize=12)

    plt.show()

if __name__ == "__main__":

    data0 = []
    file = open("dataset_circles.csv", 'r')
    for line in file:
        a = float(line.split(',')[0])
        b = float(line.split(',')[1])

        data0.append([a, b])

    c = np.random.randn(400,2)
    d = np.matrix(data0).T
    e = c*d

    data = np.array(e)
    k = 3
    center = centers(data, k)
    center, adismatrix = k_means(data, k)

    showc(data,k,center,adismatrix)
